<?php
header("content-type:text/html; charset=utf-8");
require_once('../db/db.php');

$type = $_GET['type'];
switch ($_GET['type']) {
    case 0:
        $type = "新闻";
        break;
    case 1:
        $type = "军事";
        break;
    case 2:
        $type = "娱乐";
        break;
    case 3:
        $type = "笑话";
        break;
}

//获取数据库数据
$sql = "select * from news where type_new = '$type' order by id asc ";
$news = array();//保存数据
try {
    $connect = Db::getInstance()->connect();
} catch (Exception $e) {
    echo '数据库链接失败:' . $e->getMessage();
    exit;
}
$result = mysqli_query($connect, $sql);
if ($result) {
    while ($new = mysqli_fetch_assoc($result)) {
        $news[] = $new;
    }
}

?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>新闻列表</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/list.css" rel="stylesheet">
</head>
<body>

<!-- 数据管理 -->
<div class="col-sm-9  col-md-10">
    <div data-example-id="condensed-table">
        <table class="table table-condensed">
            <thead>
            <tr>
                <th>序号</th>
                <th>类别</th>
                <th>标题</th>
                <th>内容</th>
                <th>时间</th>
                <th>操作</th>
            </tr>
            </thead>

            <tbody>

            <?php

            foreach ($news as $new) {

                echo "<tr>";
                echo "<th scope='row' class='id'>{$new['id']}</th>";
                echo "<td class='type'>{$new['type_new']}</td>";
                echo "<td class='title'>{$new['title']}</td>";
                echo "<td class=’content'>{$new['content']}</td>";
                echo "<td class='time'>{$new['ctime']}</td>";
                echo " <td class='action'>
                            <a type='button' class='btn btn-danger' href='del.php?id={$new['id']}'>删除</a>
                            <a type='button' class='btn btn-info' href='edit.php?id={$new['id']}'>修改</a>
                        </td>";
                echo "</tr>";

            }

            ?>

            </tbody>
        </table>

    </div>
</div>

</body>

</html>